package dev.hitools.utils.kts

import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.Base64
import dev.hitools.utils.kts.ByteArrayExt.toBitmap
import java.io.ByteArrayOutputStream
import java.io.IOException


object BitmapExt {
    /**
     * 转换成Base64
     */
    fun Bitmap.toBase64(format: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG): String {
        var result = ""
        val outputStream = ByteArrayOutputStream()
        try {
            compress(format, 100, outputStream)
            outputStream.flush()
            outputStream.close()
            val bitmapBytes = outputStream.toByteArray()
            result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT)
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            try {
                outputStream.flush()
                outputStream.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        return result
    }

    /**
     * 转换成Drawable
     */
    fun Bitmap.toDrawable(context: Context): Drawable {
        return BitmapDrawable(context.resources, this)
    }

    /**
     * 转换成ByteArray
     */
    fun Bitmap.toByte(format: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG): ByteArray {
        val outputStream = ByteArrayOutputStream()
        compress(format, 100, outputStream)
        return outputStream.toByteArray()
    }

    /**
     * 把Bitmap附着颜色
     */
    fun Bitmap.tint(color: Int): Bitmap {
        val canvas = Canvas(this)
        val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)

        val bgBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        val bgCanvas = Canvas(bgBitmap)
        bgCanvas.drawColor(color)
        bgCanvas.drawBitmap(bgBitmap, 0f, 0f, null)
        canvas.drawBitmap(bgBitmap, 0f, 0f, paint)
        bgBitmap.recycle()
        return this
    }

    /**
     * 旋转角度
     */
    fun Bitmap.rotate(angle: Int): Bitmap {
        val matrix = Matrix()
        matrix.postRotate(angle.toFloat())
        return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
    }

    /**
     * 压缩Bitmap
     * @param format 压缩后的格式
     * @param maxSize 最大大小
     */
    fun Bitmap.compress(format: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG, maxSize: Int = 2048): Bitmap {
        var quality = 100
        val outputStream = ByteArrayOutputStream()
        compress(format, quality, outputStream)
        // 循环判断如果压缩后图片是否大于指定大小,大于继续压缩
        while (outputStream.toByteArray().size / 1024 > maxSize) {
            outputStream.reset()
            quality -= 10
            compress(format, quality, outputStream)
        }
        return outputStream.toByteArray().toBitmap()
    }

    /**
     * 调节Bitmap的亮度
     * @param brightness 亮度的值 1-255 为增亮 ； -254 - 0 为加黑
     */
    fun Bitmap.brightness(brightness: Float): Bitmap {
        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        val cMatrix = ColorMatrix()
        cMatrix.set(floatArrayOf(1f, 0f, 0f, 0f, brightness, 0f, 1f, 0f, 0f, brightness, 0f, 0f, 1f, 0f, brightness, 0f, 0f, 0f, 1f, 0f))

        val paint = Paint()
        paint.colorFilter = ColorMatrixColorFilter(cMatrix)

        val canvas = Canvas(bitmap)
        canvas.drawBitmap(this, 0f, 0f, paint)
        return bitmap
    }
}